<?php
    include_once './config.php';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $phone = $_POST['phone'];
        $password = $_POST['password'];
        $msg = '';
        $error = false;
        if (empty($phone) || empty($password)) {
            $msg = '用户名或密码为空!';
            $error = true;
        } 
        if(!$error){
            $password = md5($password);
            $query = "select * from whw_user where phone=".quote($phone)." and password=".quote($password);
            $user = $objDBCD14->queryUniqueObject($query);
            if (!$user) {
                $msg = '账号不存在或密码不正确!';
                $error = true;
            } else {
                $user = json_decode(json_encode($user),true);
                if(!empty($_SESSION['wechat_user'])){
                    $wechat_user = $_SESSION['wechat_user'];
                    $openid = $wechat_user['original']['openid'];
                    $query = "update whw_user set nickname = '',avatar = '',openid = '' where openid = ".quote($openid);
                    $objDBCD14->execute($query);
                    $query = "update whw_user set nickname = ".quote($wechat_user['nickname']).",avatar = ".quote($wechat_user['avatar']).",openid = ".quote($openid)." where id=".getInt($user['id']);
                    $objDBCD14->execute($query);
                }
                $expire=time()+60*60*24*30;
                setcookie("user", json_encode($user), $expire);
                $msg = "登录成功";
            }
        }
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg,'type'=>$error ? '' : $user->type]);
        exit;
    } 
    
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>登录</title>
    <link rel="stylesheet" href="weui/weui.min.css"/>
    <link rel="stylesheet" href="weui/example.css?t=<?= time() ?>"/>
    <style>
        .container{
            margin: 0 20px;
        }
        form{
            padding: 0 20px 20px;
        }
        .weui-cells:after, .weui-cells:before,.weui-cell:before{
            display: none;
        }
        .weui-cell{
            border: 1px solid #eee;
            border-radius: 5px;
            margin-top: 15px;
        }
        .weui-dialog__title{
            margin-top: 20px;
        }
        .weui-btn-area{
            margin: 30px 0 10px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <div class="input js_show">
            <div class="weui-cells weui-cells_form">
                <form id="bindForm" method="post">
                    <div style="padding: 20px 0 0;text-align: center;font-weight:900">
                        <?=!empty($_SESSION['wechat_user']) ? '绑定账号' : '登录账号'?>
                    </div>
                    <div class="weui-dialog__title">用户名:</div>
                    <div class="weui-cell">
                        <div class="weui-cell__bd">
                                <input class="weui-input" name="phone" type="text" placeholder="请输入登录账号"/>
                        </div>
                    </div>
                    <div class="weui-dialog__title">密码:</div>
                    <div class="weui-cell">
                        <div class="weui-cell__bd">
                            <input class="weui-input" name="password" type="password" placeholder="请输入密码" />
                        </div>
                    </div>
                    <div class="weui-btn-area">
                        <button class="weui-btn weui-btn_warn" type="submit">登录</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div role="alert" id="textToast" style= "display: none;">
         <div class="weui-mask_transparent"></div>
        <div class="weui-toast weui-toast_text">
            <p class="weui-toast__content">文字提示</p>
        </div>
    </div>
    <script type="text/javascript" src="weui/jquery.min.js"></script>
    <script type="text/javascript" src="weui/jquery-weui.min.js"></script>
    <script>
       var $toast = $('#textToast');
        $(function () {
            $("#bindForm").submit(function(){
              var formValues = $("#bindForm").serializeArray();
              var param = {}
              for(var i = 0;i < formValues.length;i++){
                    param[formValues[i].name] = formValues[i].value
              }
              $.post("login.php",param,function(res){
                    res = JSON.parse(res)
                    $(".weui-toast__content").text(res.msg)
                    if ($toast.css('display') != 'none') return;
                    $toast.fadeIn(100);
                    setTimeout(function () {
                        $toast.fadeOut(100);
                        if(res.code == 1){
                            window.close();
                            window.location.href = res.type == 1 ? 'index.php' : 'worker.php';
                        }
                    }, 1000);
              })
              return false
            })
        })
    </script>  
</body>
</html>

